package com.superteam.mac.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.RowMapper;

import com.superteam.mac.mode.MachineRec;
import com.superteam.mac.mode.WorkShop;

public class WorkShopDAO extends BaseDAO {

	public int findTotalWorkShop()
	{
		String sql = "select count(FactoryName) from tbworkshop";
		return this.getJdbcTemplate().queryForInt(sql);
	} 
	
	@SuppressWarnings("unchecked")
	public List findAllWorkShop(int startIndex,int count)
	{
		List<WorkShop>  list = this.getJdbcTemplate().query(
			    "select * from tbworkshop",
			    new RowMapper() {

			        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			        	WorkShop work = new WorkShop();
			        	
			        	work.setWorkShopCode(rs.getString("WorkshopCode"));
			        	work.setWorkShopName(rs.getString("WorkshopName"));
			        	work.setLogName(rs.getString("LogName"));
			        	work.setLogCode(rs.getString("LogCode"));
			        	work.setIpAddress(rs.getString("IpAddress"));
			            return work;
			        }
			    });
		return list;
	}
	
	public WorkShop findByCode(String cod)
	{
		try
		{
			WorkShop work = (WorkShop) this.getJdbcTemplate().queryForObject(
			    "select * from tbworkshop where WorkshopCode = ? ",
			    new Object[]{cod},
			    new RowMapper() {

			        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			        	WorkShop work = new WorkShop();
			        	work.setWorkShopCode(rs.getString("WorkshopCode"));
			        	work.setWorkShopName(rs.getString("WorkshopName"));
			        	work.setLogName(rs.getString("LogName"));
			        	work.setLogCode(rs.getString("LogCode"));
			        	work.setIpAddress(rs.getString("IpAddress"));
			        	work.setLayoutMap(rs.getBlob("LayoutMap"));
			            return work;
			        }
			    });
		return work;
		}catch(Exception e)
		{
			return null;
		}
	}
	
	public boolean dealWorkShop(String cod)
	{
		String sql = "delete from tbworkshop where WorkshopCode = ?";
		try
		{
			this.getJdbcTemplate().update(sql,new Object[]{cod});
			
			return true;
		}catch(Exception e)
		{
			return false;
		}
	}
	public boolean addWorkShop(final WorkShop work)
	{
		try
		{
			String sql = "insert into tbworkshop(WorkshopCode,WorkshopName,LogName,LogCode,IpAddress,LayoutMap,FactoryName) values(?,?,?,?,?,?,?)";
			
			getJdbcTemplate().execute(sql, 
				     new PreparedStatementCallback(){
				     public Object doInPreparedStatement(PreparedStatement stmt)
				        throws SQLException, DataAccessException {
				         stmt.setString(1, work.getWorkShopCode());
				         stmt.setString(2, work.getWorkShopName());
				         stmt.setString(3, work.getLogName());
				         stmt.setString(4, work.getLogCode());
				         stmt.setString(5, work.getIpAddress());
				         stmt.setBinaryStream(6, work.getLayoutMap().getBinaryStream(), (int)work.getLayoutMap().length());//将图片保存
				         stmt.setString(7,"实验加工厂");
				         stmt.execute();
				         return true;
				        }
				       });

			return true;
			/*this.getJdbcTemplate().update(sql, new Object[]{work.getWorkShopCode(),work.getWorkShopName(),work.getLogName(),work.getLogCode(),work.getIpAddress(),work.getLayoutMap()});
			return true;*/
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<WorkShop> findWorkShopAndMachines()
	{
		List<WorkShop> works = this.findAllWorkShop(0, 100);
		for(WorkShop work:works)
		{
			
			String sql = "select * from tbMachineRec where WorkshopCode = ?";
			List<MachineRec>  list = this.getJdbcTemplate().query(
				    sql, new Object[]{work.getWorkShopName()},
				    new RowMapper() {

				        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				        	MachineRec machine = new MachineRec();
				        	machine.setWorkshopCode(rs.getString("WorkshopCode"));
				        	machine.setMachineName(rs.getString("MachineName"));
				        	machine.setMachineCode(rs.getString("MachineCode"));
				        	machine.setModelType(rs.getString("ModelType"));
				        	machine.setFlMapX(rs.getInt("FLMapX"));
				        	machine.setFlMapY(rs.getInt("FLMapY"));
				        	machine.setWlMapX(rs.getInt("WLMapX"));
				        	machine.setWlMapY(rs.getInt("WLMapY"));
				        	machine.setDescription(rs.getString("Description"));
				        	
				            return machine;
				        }
				    });
			//System.out.println(list.size());
			work.setMachines(list);
		}
		return works;

	}
}
